﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class SmsSettings : Form
    {
        public SmsSettings()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxUserName.Text.Length > 0 && textBoxPassword.Text.Length > 0 && richTextBox1.Text.Length > 0 && richTextBoxBdayMsg.Text.Length > 0)
                {
                    saveSmsSettings(textBoxUserName.Text.ToString().Trim(), textBoxPassword.Text.ToString().Trim(), richTextBox1.Text.ToString().Trim(), richTextBoxBdayMsg.Text.ToString().Trim());
                }
                else
                {
                    MessageBox.Show("Please provide username and password", "Sms Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show("provided username and password", "Sms Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            
        }

        private void saveSmsSettings(String userName, String password,String remainderMsg,String bdayWishMsg)
        {
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                config.AppSettings.Settings["SmsUserName"].Value = userName;
                config.AppSettings.Settings["SmsPassword"].Value = password;
                config.AppSettings.Settings["RemainderMessage"].Value = remainderMsg;
                config.AppSettings.Settings["BirthdayWishMessage"].Value = bdayWishMsg;
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
                MessageBox.Show("Successfully saved sms settings", "Sms Settings", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch
            {
                MessageBox.Show("Failed to save sms settings", "Sms Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {

            }
           
      
        }

        private void readSmsSettings()
        {
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                textBoxUserName.Text = config.AppSettings.Settings["SmsUserName"].Value;
                textBoxPassword.Text = config.AppSettings.Settings["SmsPassword"].Value;
                richTextBox1.Text  = config.AppSettings.Settings["RemainderMessage"].Value;
                richTextBoxBdayMsg.Text = config.AppSettings.Settings["BirthdayWishMessage"].Value;
            }
            catch
            {
                MessageBox.Show("Failed to read sms settings", "Sms Settings", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
           
        }

        private void SmsSettings_Load(object sender, EventArgs e)
        {
            readSmsSettings();
        }
    }
}
